/*
 * drv832x.c
 *
 *  Created on: Nov 19, 2024
 *      Author: Vinson
 */
#include "drv832x.h"
#include "global_var.h"
#include "micros.h"

static void drv832x_write_reg(uint8_t reg, uint16_t val)
{
    val |= reg << 11;

    gpio_set_value(&drv_cs, 0);
    HAL_SPI_Transmit(&hspi3, (uint8_t *)&val, 1, HAL_MAX_DELAY);
    gpio_set_value(&drv_cs, 1);
}


static uint16_t drv832x_read_reg(uint8_t reg)
{
    uint16_t rx_val;
    uint16_t val = 0x8000 | reg << 11;

    gpio_set_value(&drv_cs, 0);
    HAL_SPI_TransmitReceive(&hspi3, (uint8_t *)&val, (uint8_t *)&rx_val, 1, HAL_MAX_DELAY);
    gpio_set_value(&drv_cs, 1);
    return rx_val & 0x7ff;
}

void drv832x_init()
{
	gpio_set_value(&drv_en, 1);
	delay_us(50 * 1000);
	drv832x_write_reg(0x02, (1 << 10) | (1 << 7) | (1 << 5)); // shutdown all on err, otw err, 3x pwm mod
	delay_us(50 * 1000);
}
